<script lang="ts">
export default {name: 'Dashboard'};
</script>

<script lang="ts" setup>
// 组件引用
import SvgIcon from '@/components/SvgIcon/index.vue';
import {onMounted, reactive, toRefs} from "vue";

import {CardPanel} from "@/api/home/types";
import {RouteRecord, useRouter} from "vue-router";
import router from '@/router'
import {queryRecordCounts} from "@/api/cloud/table/record";
import {getAttribute} from "@/utils/objutil";


const dataState = reactive({
  cards: [] as CardPanel[],
});

const {
  cards,
} = toRefs(dataState);

/**
 * 刷新页面信息
 */
function handleQuery() {
  let entityList = [] as string[];
  for (let card of cards.value) {
    entityList.push(card.entity);
  }

  queryRecordCounts(entityList).then(({data}) => {
    for (let entity of Object.keys(data)) {
      let count = getAttribute(data, entity);
      for (let card of cards.value) {
        if (card.entity === entity) {
          card.number = count;
        }
      }

    }
  });
}

/**
 * 从路由表中，提取并构造卡片信息
 */
function getCardPanel() {
  // 获得当前路由信息：也就是地址栏上的信息，其中的查询参数带有EntityType
  const router = useRouter();

  let cards = [] as CardPanel[];

  const routes = router.getRoutes() as RouteRecord[];
  for (let route of routes) {
    // 一级菜单的Path
    if (!route.path.startsWith('/cloud/cloud/manager')) {
      continue;
    }

    // 二级菜单的Path
    let path2 = route.path.substring('/cloud/cloud/manager'.length);
    let menu2 = path2.substring(0, path2.indexOf('/cloud/manager/'));

    // 三级菜单:实体名称
    let path3 = path2.substring(menu2.length);
    let menu3 = path3.substring('/cloud/manager/'.length);

    let title = route.meta.title as string;

    let card = {} as CardPanel;
    card.label = title;
    card.entity = menu3;
    card.number = 0;
    card.url = route.path + '?name=' + card.entity;

    cards.push(card);
  }

  return cards;
}

/**
 * 切换页面
 * @param item
 */
function reroute(item: CardPanel) {
  router.push(item.url);
}

/**
 * 响应页面安装：页面的初始化工作
 */
onMounted(() => {
  cards.value = getCardPanel();

  // 查询实体数据
  handleQuery();

});

</script>

<template>
  <div class="dashboard-container">
    <!-- 数据 -->
    <el-row :gutter="40" class="card-panel__col">
      <el-col v-for="item in cards" :lg="6" :sm="12" :xs="24"
              class="card-panel__col"
      >
        <div class="card-panel" @click="reroute(item)">
          <div class="card-panel-icon-wrapper icon-blue">
            <svg-icon icon-class="table" size="4em"/>
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">{{ item.label }}</div>
            <div class="card-panel-num">{{ item.number }}</div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.dashboard-container {
  padding: 24px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0;
    border: 0;
    right: 0;
    z-index: 99;
  }

  .box-center {
    margin: 0 auto;
    display: table;
  }

  .user-profile {
    .box-center {
      padding-top: 10px;
    }

    .user-role {
      padding-top: 10px;
      font-weight: 400;
      font-size: 14px;
    }

    .box-social {
      padding-top: 30px;

      .el-table {
        border-top: 1px solid #dfe6ec;
      }
    }

    .user-follow {
      padding-top: 20px;
    }
  }

  .card-panel__col {
    margin-bottom: 12px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    border-color: rgba(0, 0, 0, 0.05);

    .icon-blue {
      color: #36a3f7;
    }

    .icon-black {
      color: #8c939d;
    }

    .icon-pink {
      color: #f4516c;
    }

    .icon-green {
      color: #34bfa3;
    }

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-black {
        background: #8c939d;
      }

      .icon-blue {
        background: #36a3f7;
      }

      .icon-pink {
        background: #f4516c;
      }

      .icon-green {
        background: #34bfa3;
      }
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px 20px 0;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
        text-align: right;
      }
    }
  }

  .chart-container {
    background: #ffffff;
  }
}
</style>
